<?php 
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
class manage extends admin {
	function __construct(){
		parent::__construct();
		$this->order_db=pc_base::load_model('order_model');
		$this->yubao_db=pc_base::load_model('yubao_model');
		$this->M = new_html_special_chars(getcache('manage', 'commons'));
		$this->complaint_db=pc_base::load_model('complaint_model');
	}
	/*
	 * 默认显示,为订单查看。
	 * 
	 */
	function init(){
		//echo mktime();
		$pn=isset($_GET['p'])?max(intval($_GET['p']),1):1;
		$pagesize=5;
		$num=$this->order_db->count('id');
		$mp=max(ceil($num/$pagesize),1);
		$pn=min($pn,$mp);
		$kaishi=($pn-1)*$pagesize;
		$ps=max($pn-1,1);
		$px=$pn+1;
		$limit="$kaishi,$pagesize";
		$datas=$this->order_db->select($where = '', $data = '*', $limit, $order = 'id desc', $group = '', $key='');
		$pages="<a href='index.php?m=order&c=manage&p=$ps&menuid=1669'>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?m=order&c=manage&p=$px&menuid=1669'>下一页</a>";
		//echo $pages;
		include $this->admin_tpl('manage_order');
	}
	/*
	 * 到货预报查看
	*
	*/
	function dhyb(){
		//unset($show_header);//后台显示导航条；
		$pagesize=5;		
		$pn=isset($_GET['p'])?max(intval($_GET['p']),1):1;
		$num=$this->yubao_db->count('id');
		$mp=max(ceil($num/$pagesize),1);
		$pn=min($pn,$mp);
		$kaishi=($pn-1)*$pagesize;
		$ps=max($pn-1,1);
		$px=$pn+1;
		$my_sql="select * from v9_yubao order by id desc limit $kaishi,$pagesize ";
		//echo $my_sql;
		$res=$this->order_db->query($my_sql);
		$datas=$this->order_db->fetch_array($res);
		//print_r($datas);		
		$pages="<a href='index.php?m=order&c=manage&a=dhyb&p=$ps&menuid=1669'>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?m=order&c=manage&a=dhyb&p=$px&menuid=1669'>下一页</a>";		
		if($mp==1)unset($pages);
		include $this->admin_tpl('yubao');
	}
	/*
	 * 订单查看
	 * 
	 */
	function ddck(){
		
		include $this->admin_tpl('view_order');
	}
	/*
	 * 改变订单状态
	*
	*/
	function ddzt(){
		$orderid=isset($_GET['orderid'])?safe_replace($_GET['orderid']):exit('操作非法');
		if(isset($_POST['orderid'])){
			$orderid=$_POST['orderid'];
			$check=$this->order_db->get_one("id=$orderid",'state');
			if($check==7){
				echo '权限不够！联系管理员';
				exit();
			}
			$state=$_POST['state'];
			
			$res=$this->order_db->update("state=$state","id=$orderid");
			
		}
		$this->detail_db=pc_base::load_model('catgoods_model');
		$this->channel_db=pc_base::load_model('channel_model');
		$channel=$this->channel_db->select('id!=0','mark');
		//print_r($channel);
		$data_1=$this->order_db->get_one("id=$orderid");
		//print_r($order);
		$zhnumber=$data_1['zhnumber'];
		$chid=$data_1['channel']-1;
		$data_1['channel']=$channel[$chid]['mark'];
		$leishu=$this->detail_db->count("zhnumber='{$zhnumber}'");
		$data_3=$this->detail_db->select("zhnumber='$zhnumber'");
		$data_2=$this->yubao_db->get_one("zhnumber='$zhnumber'");
		$zongjia=$data_1['orderprice']+$data_1['tariff']+$data_2['whprice'];
		$address=unserialize($data_1['shaddress']);
		include $this->admin_tpl('change_state');
	}
	
	/*
	 *搜索单号
	*
	*/
	function ssdd(){
		$mp=1;		
		if(isset($_GET['dosubmit'])){
			//print_r($_POST);
			$skey=isset($_GET['tbKey'])||$_GET['tbKey']==''?safe_replace($_GET['tbKey']):exit('请开启javascirpt');
			$leibie=isset($_GET['leibie'])?safe_replace($_GET['leibie']):3;
			switch ($leibie){
				case 1:{
					$leibie='orderid';
					$where="$leibie like '%{$skey}%'";
					break;
				}
				case 2:{
					$leibie='zhnumber';
					$where="$leibie like '%{$skey}%'";
					break;
				}
				case 3:{
					$where="orderid like '%{$skey}%' or zhnumber like '%{$skey}%'";
				}
			}
			$stime=isset($_GET['tbStart'])?safe_replace($_GET['tbStart']):'';
			$etime=isset($_GET['tbEnd'])?safe_replace($_GET['tbEnd']):'';
			$preg='/^\d{4}-\d{1,2}-\d{1,2}$/';
			if(preg_match($preg,$stime)){
				$stime=explode("-",$stime);
				$stime=mktime(0,0,0,$stime[1],$stime[2],$stime[0]);
				if(preg_match($preg,$etime)){
					$etime=explode("-",$etime);
					$etime=mktime(23,59,59,$etime[1],$etime[2],$etime[0]);
					$where.="and uptime<$etime and uptime>$stime";
				}else{
					$where.="and uptime>$stime";
				}
			}else{
				if(preg_match($preg,$etime)){
					$etime=explode("-",$etime);
					$etime=mktime(23,59,59,$etime[1],$etime[2],$etime[0]);
					$where.="and uptime<$etime";
				}else{
					$where=$where;
				}
			}
	
			$pagesize=5;
			$pn=isset($_GET['p'])?max(intval($_GET['p']),1):1;
			$num=$this->order_db->count("$where");
			$mp=max(ceil($num/$pagesize),1);
			//echo $mp;
			$pn=min($pn,$mp);
			$kaishi=($pn-1)*$pagesize;
			$ps=max($pn-1,1);
			$px=$pn+1;
			$limit="$kaishi,$pagesize";
			$datas=$this->order_db->select($where,'*',$limit);
	    }		
		//print_r($datas);
		$pages="<a href='index.php?m=order&c=manage&a=ssdd&p=$ps&menuid=1669&dosubmit=1&tbKey=$skey&leibie=$leibie&tbStart=$stime&tbEnd=$etime'>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?m=order&c=manage&a=ssdd&p=$px&menuid=1669&dosubmit=1&tbKey=$skey&leibie=$leibie&tbStart=$stime&tbEnd=$etime'>下一页</a>";
		if($mp==1)unset($pages);
		include $this->admin_tpl('search_order');
	}
	/*
	 *查看投诉
	*
	*/
	function ckts(){
		
		$mp=1;
		if(isset($_GET['id'])){
			
		}
		else{
			$pagesize=5;
			$pn=isset($_GET['p'])?max(intval($_GET['p']),1):1;
			$num=$this->complaint_db->count('id');
			$mp=max(ceil($num/$pagesize),1);
			$pn=min($pn,$mp);
			$kaishi=($pn-1)*$pagesize;
			$ps=max($pn-1,1);
			$px=$pn+1;
			$limit="$kaishi,$pagesize";
			$datas=$this->complaint_db->select('','*',$limit);
			$pages="<a href='index.php?m=order&c=manage&a=ckts&p=$ps&menuid=1669'>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?m=order&c=manage&a=ckts&p=$px&menuid=1669'>下一页</a>";
		}
		if($mp==1)unset($pages);
		include $this->admin_tpl('view_complaint');
	}
	/*
	 * 投诉单处理
	 * 
	 */
	function tscl(){
		if(isset($_POST['dosubmit'])){
			//print_r($_POST);
			$state=isset($_POST['state'])?safe_replace($_POST['state']):exit('出现异常错误，请刷新2。');
			$id=isset($_POST['id'])?safe_replace($_POST['id']):exit('出现异常错误，请刷新3。');
			$this->complaint_db->update("state='$state'","id=$id");
		
		}else{
			$id=isset($_GET['id'])?safe_replace($_GET['id']):exit('出现异常错误，请刷新1。');
			$data=$this->complaint_db->get_one("id=$id");
			include $this->admin_tpl('deal_complaint');
		}	
		
		
	}
	/*
	 *管理用户地址，提醒用户那些地址出错
	*
	*/
	function gldz(){
		$this->address_db=pc_base::load_model('address_model');
		$mp=1;
		if(isset($_GET['dosubmit'])){
			$tbkey=isset($_GET['username'])?safe_replace($_GET['username']):exit('输入查询的用户名');
			$where="username like '%$tbkey%'";
			$pagesize=5;
			$pn=isset($_GET['p'])?max(intval($_GET['p']),1):1;
			$num=$this->address_db->count("$where");
			$mp=max(ceil($num/$pagesize),1);
			//echo $mp;
			$pn=min($pn,$mp);
			$kaishi=($pn-1)*$pagesize;
			$ps=max($pn-1,1);
			$px=$pn+1;
			$limit="$kaishi,$pagesize";
			$datas=$this->address_db->select($where,'*',$limit);
			$pages="<a href='index.php?m=order&c=manage&a=gldz&p=$ps&menuid=1669&dosubmit=1&username=$tbkey'>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='index.php?m=order&c=manage&a=gldz&p=$px&menuid=1669&dosubmit=1&&username=$tbkey'>下一页</a>";
		}
		if($mp==1)unset($pages);
		include $this->admin_tpl('manage_address');
	}
	function glhl(){
		$this->rmb_db=pc_base::load_model('rmb_model');
		if(isset($_POST['dosubmit'])){
			//echo 'yy';
			$inhl=preg_match('/^[\d]+(\.)?[\d]{1}$/',$_POST['rmb'])?$_POST['rmb']:exit('汇率格式不正确');			
			$this->rmb_db->update("rmb=$inhl","id=1");			
		}		
		$rmb=$this->rmb_db->get_one('id=1','rmb');
		include $this->admin_tpl('change_rate');;
	}
}
	

?>